package org.cwnuit.train.acton;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.cwnuit.train.domain.Learns;
import org.cwnuit.train.domain.Rules;
import org.cwnuit.train.domain.Sworks;
import org.cwnuit.train.domain.Users;
import org.cwnuit.train.service.LearnService;
import org.cwnuit.train.service.RulesService;
import org.cwnuit.train.service.SworksService;
import org.cwnuit.train.service.TrainsService;
import org.cwnuit.train.utils.Today;
import org.cwnuit.train.vo.LearnSworkRuleVO;
@SuppressWarnings("all")
public class LearnAction extends HttpServlet {
    
	LearnService learnService = new LearnService();
	RulesService rulesService = new RulesService();
	
	TrainsService trainService = new TrainsService();

	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String cmd = request.getParameter("cmd");
		if (cmd.equals("read_tdb")) {//铁道部
			int rid = Integer.parseInt(request.getParameter("rid"));
			request.setAttribute("action", "read_tdb");
			request.setAttribute("rule", findRule(rid));
			request.getRequestDispatcher("web/learn/detail.jsp").forward(request, response);
		} else if (cmd.equals("read_tlj")) {//铁路局
			int rid = Integer.parseInt(request.getParameter("rid"));
			request.setAttribute("action", "read_tlj");
			request.setAttribute("rule", findRule(rid));
			request.getRequestDispatcher("web/learn/detail.jsp").forward(request, response);
		} else if (cmd.equals("read_cwd")) {//车务段
			int rid = Integer.parseInt(request.getParameter("rid"));
			request.setAttribute("action", "read_cwd");
			request.setAttribute("rule", findRule(rid));
			request.getRequestDispatcher("web/learn/detail.jsp").forward(request, response);
		} else if (cmd.equals("readfinish")) {
			int lid = Integer.parseInt(request.getParameter("lid"));
			String tid = request.getParameter("tid");
			String rid = request.getParameter("rid");
			String msg = "";
			if (lid == 0) {
				msg = "恭喜你，你已经成功阅读了一篇规章制度！";
				addLearn(request);
			} else {
				msg = "恭喜你，你已经再次阅读了一篇规章制度，阅读次数+1！";
				updateLearn(request);
			}
			request.getSession().setAttribute("msg",msg);
			response.sendRedirect("common?cmd=reader&rid=" + rid + "&tid=" + tid);
		}
	}
	
	
	// 查找规章信息
	private Rules findRule(int rid) {
		String SQL = "SELECT * FROM Rules WHERE rid = " + rid;
		return rulesService.findRule(SQL);
	}
	// 添加学习情况信息
	private boolean addLearn(HttpServletRequest request) {
		int sid = Integer.parseInt(request.getParameter("sid"));
		String SQL = "INSERT INTO learns(sid,uid,lnum,lastltime) VALUES("
				+ sid
				+ "," 
				+ ((Users)request.getSession().getAttribute("manager")).getUid()
				+ ",1,'"
				+ new Today().getAllDateOfNum() + "')";
		
		return learnService.addLearn(SQL);
	}
	
	// 修改学习情况信息
	private boolean updateLearn(HttpServletRequest request) {
		int lid = Integer.parseInt(request.getParameter("lid"));
		Learns learn = learnService.findLearn("SELECT * FROM Learns WHERE lid = " + lid );
		int lnum = learn.getLnum()+1;
		String SQL = "UPDATE Learns SET lnum = "+lnum+" WHERE lid = " + lid;
		return learnService.updateLearn(SQL);
	}
}
